package cn.xhy.material.controller.admin.depot;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.xhy.material.controller.admin.depot.vo.DepotPageReqVO;
import cn.xhy.material.controller.admin.depot.vo.DepotRespVO;
import cn.xhy.material.controller.admin.depot.vo.DepotSaveReqVO;
import cn.xhy.material.dal.dataobject.depot.DepotDO;
import cn.xhy.material.service.depot.DepotService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
@Tag(name = "管理后台 - 仓库")
@RestController
@RequestMapping("/material/depot")
@Validated
public class DepotController {

    @Resource
    private DepotService depotService;

    @PostMapping("/create")
    @Operation(summary = "创建仓库")
    @PreAuthorize("@ss.hasPermission('material:depot:create')")
    public CommonResult<Long> createDepot(@Valid @RequestBody DepotSaveReqVO createReqVO) {
        return success(depotService.createDepot(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新仓库")
    @PreAuthorize("@ss.hasPermission('material:depot:update')")
    public CommonResult<Boolean> updateDepot(@Valid @RequestBody DepotSaveReqVO updateReqVO) {
        depotService.updateDepot(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除仓库")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('material:depot:delete')")
    public CommonResult<Boolean> deleteDepot(@RequestParam("id") Long id) {
        depotService.deleteDepot(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得仓库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('material:depot:query')")
    public CommonResult<DepotRespVO> getDepot(@RequestParam("id") Long id) {
        DepotDO depot = depotService.getDepot(id);
        return success(BeanUtils.toBean(depot, DepotRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得仓库分页")
    @PreAuthorize("@ss.hasPermission('material:depot:query')")
    public CommonResult<PageResult<DepotRespVO>> getDepotPage(@Valid DepotPageReqVO pageReqVO) {
        PageResult<DepotDO> pageResult = depotService.getDepotPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DepotRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出仓库 Excel")
    @PreAuthorize("@ss.hasPermission('material:depot:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDepotExcel(@Valid DepotPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DepotDO> list = depotService.getDepotPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "仓库.xls", "数据", DepotRespVO.class,
                        BeanUtils.toBean(list, DepotRespVO.class));
    }

}
